/*
 *  Copyright 2013 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef PROTOQTRPC_LOCATION_LOCATIONSERVICEIMPL_H
#define PROTOQTRPC_LOCATION_LOCATIONSERVICEIMPL_H

#include <QMutex>
#include "protoqtrpc/location/LocationService.pb.h"

class QString;
class QSqlQuery;

namespace protoqtrpc {
namespace location {

class LocationServiceImpl: public LocationService
{
public:
    LocationServiceImpl();
    virtual ~LocationServiceImpl();

    virtual void registerServer(::google::protobuf::RpcController* controller,
                         const ServerRegistrationRequest* request,
                         ServerRegistrationResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void unregisterServer(::google::protobuf::RpcController* controller,
                         const NameRequest* request,
                         GenericResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void getServerInfo(::google::protobuf::RpcController* controller,
                         const NameRequest* request,
                         ServerInfoResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void getServerServicesInfo(::google::protobuf::RpcController* controller,
                                        const NameRequest* request,
                                        ServerServicesInfoResponse* response,
                                        ::google::protobuf::Closure* done);

    virtual void getServersInfo(::google::protobuf::RpcController* controller,
                                 const VoidRequest* request,
                                 ServersInfoResponse* response,
                                 ::google::protobuf::Closure* done);


    virtual void registerService(::google::protobuf::RpcController* controller,
                         const ServiceRegistrationRequest* request,
                         GenericResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void unregisterService(::google::protobuf::RpcController* controller,
                         const ServiceId* request,
                         GenericResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void getServiceInfo(::google::protobuf::RpcController* controller,
                         const ServiceId* request,
                         ServiceInfoResponse* response,
                         ::google::protobuf::Closure* done);

    virtual void locateService(::google::protobuf::RpcController* controller,
                         const LocateServiceRequest* request,
                         LocateServiceResponse* response,
                         ::google::protobuf::Closure* done);

private:
    Status getServerId(QSqlQuery& query, quint32 host, const QString& server,
                        int &serverId);

private:
    QMutex mMutex;
};

}
}

#endif
